package com.kgd.news.controller.admin;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.upload.UploadFile;
import com.kgd.news.entity.Adv;
import com.kgd.news.entity.Category;
import com.kgd.news.utils.BaseController;
import com.kgd.news.utils.StringUtils;

import static com.kgd.news.utils.Constants.PAGE_SIZE;

/**
 * @Email: huangsanm@foxmail.com
 * @Date: 2017/8/14
 * @Time: 下午11:25
 *
 * 20180119:
 */
public class CategoryController extends BaseController {

    @Override
    public void list() {
        String sql = "from category where 1 = 1";
        String parentid = getPara("parentid");
        String type = "category";
        if (!StringUtils.isEmpty(parentid)) {
            if ("-1".equals(parentid)) {
                sql += " and parentid is null" ;
            } else {
                sql += " and parentid =" + parentid;
            }
            type += parentid;
        }
        sql += " and parentid<5 order by id desc";
        Page<Category> data = Category.dao.paginate(getPageIndex(type), PAGE_SIZE, "select *", sql);

        setAttr("data", data);
        setAttr("parentid", parentid);
        render("category_list.html");
    }

    @Override
    public void edit() {
        Integer id = getParaToInt("id");
        Category category = Category.dao.findById(id);
        setAttr("parentid", getPara("parentid"));
        setAttr("category", category);
        render("category_edit.html");
    }

    @Override
    public void save() {
        UploadFile uploadFile = getFile("category.icon");
        String filePath = "";
        if (uploadFile != null && !StringUtils.isEmpty(uploadFile.getFileName())) {
            filePath = getFileUploadPath(uploadFile);
        }
        Category us = getModel(Category.class, "category");
        if (!StringUtils.isEmpty(filePath)) {
            us.setIcon(filePath);
        }
        if(us != null && us.getId() != null){

            us.update();
        }else{
            us.setIcon(filePath);
            us.save();
        }
        CacheKit.removeAll("category");

        list();
    }

    public void addAdv() {
        Integer categoryid = getParaToInt("categoryid");
        setAttr("categoryid", categoryid);
        setAttr("parentid", getPara("parentid"));
        render("add_adv.html");
    }

    public void saveAdv() {
        UploadFile uploadFile = getFile("img");
        String filePath = "";
        if (uploadFile != null && !StringUtils.isEmpty(uploadFile.getFileName())) {
            filePath = getFileUploadPath(uploadFile);
        }
        Adv us = getModel(Adv.class, "adv");
        if (!StringUtils.isEmpty(filePath)) {
            us.setImg(filePath);
        }
        if (!StringUtils.isEmpty(filePath)) {
            us.setImg(filePath);
        }
        us.save();
        setAttr("parentid", getPara("parentid"));
        list();
    }

    @Override
    public void del() {
        String ids = getPara("ids");
        Db.update("delete from category where id in (" + ids + ")");

        list();
    }
}
